﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;

public partial class UserControl_Role_Chucnang : System.Web.UI.UserControl
{
    #region Public Methods
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion

    #region Data Structure
    #endregion

    #region Members
    DAChucNang objChucNang;
    #endregion

    #region Private Methods
    private void loadChucNangCha()
    {
        DataSet ds_objChucNang = objChucNang.LayDanhSachChucNangCha();      
        if (ds_objChucNang != null && ds_objChucNang.Tables[0].Rows.Count > 0)
        {
            // Chi ra ddl biet, hien thi du lieu o cot nao trong kho du lieu
            ddlChucNangCha.DataTextField = "TenChucNang";
            ddlChucNangCha.DataValueField = "ID";
            // Chi ra cho ddl biet lay nguon du lieu o dau
            ddlChucNangCha.DataSource = ds_objChucNang.Tables[0];
            ddlChucNangCha.DataBind();
        }
    }
    private void loadChucNangContheoChucNangCha()
    {
        int v_temp = Convert.ToInt32(ddlChucNangCha.SelectedValue);
        DataSet ds_objChucNangCon = objChucNang.LayDanhSachChucNangCon(v_temp);
        grvChucNang.DataSource = ds_objChucNangCon.Tables[0];
        grvChucNang.DataBind();
    }
    private void loadViTri()
    {
        for (int v_i = 1; v_i <= 20; v_i++)
        {
            ListItem v_lTemp = new ListItem(v_i.ToString(),v_i.ToString());
            ddlViTri.Items.Add(v_lTemp);
        }
    }
    private void AddDanhSachChucNang()
    {
        int v_i_objChucNangCon = objChucNang.ThemDanhSachChucNang(txtTenChucNang.Text.ToString()
            ,txtTenForm.Text.ToString()
            ,"true"
            ,Convert.ToInt32(ddlViTri.SelectedValue)
            ,Convert.ToInt32(ddlChucNangCha.SelectedValue)
            ,"true"
            );
        if (v_i_objChucNangCon == 1)
        {
            lblMess.Text = "Thêm bản ghi thành công";
            loadChucNangContheoChucNangCha();
        }
        else lblMess.Text = "Hãy thử lại";
    }
    private void DeleteDanhSachChucNang(int i_i_index)
    {
        int intChucNangID = Convert.ToInt32(grvChucNang.DataKeys[i_i_index].Value);
        int intChẹck = objChucNang.XoaDanhSachChucNang(intChucNangID);
        if (intChẹck > 0)
        {
            this.lblMess.Text = "Xóa thành công bản ghi";
            txtTenChucNang.Text = "";
            txtTenForm.Text = "";
            loadChucNangContheoChucNangCha();
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình xóa bản ghi!";
        }
    }
    private void loadChucNangUpdate(int v_i_index)
    {
        int v_i_id = Convert.ToInt32(grvChucNang.DataKeys[v_i_index].Value.ToString());
        if (v_i_id == 48)
        {
            lblMess.Text = "Không được sửa chức năng này";
            return;
        }
        else
        {
            DataRow v_dr_rowchucnang = objChucNang.Lay_hang_chuc_nang_by_id(v_i_id);
            txtTenChucNang.Text = v_dr_rowchucnang["TenChucNang"].ToString().Trim();
            txtTenForm.Text = v_dr_rowchucnang["TenForm"].ToString().Trim();
            ddlViTri.SelectedValue = v_dr_rowchucnang["ViTri"].ToString();//HIEN TEN TAI VI TRI DA CHON
            m_i_id.Value = v_dr_rowchucnang["ID"].ToString();
            cbTrangThai.Checked = true;
            cbHienThi.Checked = true;
        }

    }
    private void UpdateDanhSachChucNang()
    {
        int v_i_capnhat = objChucNang.CapNhatChucNang(Convert.ToInt32(m_i_id.Value)
            , txtTenChucNang.Text.ToString()
            , txtTenForm.Text.ToString()
            , "true"
            , Convert.ToInt32(ddlViTri.SelectedValue)
            ,ddlChucNangCha.SelectedValue
            , "true");
        if (v_i_capnhat > 0)
        {
            this.lblMess.Text = "Cập nhật thành công bản ghi";
            this.txtTenChucNang.Text = "";
            this.txtTenForm.Text = "";
            this.ddlViTri.SelectedValue = "1";
            this.btnUpdate.Visible = false;
            this.btnInsert.Visible = true;
            cbTrangThai.Checked = false;
            cbHienThi.Checked = false;
            loadChucNangContheoChucNangCha();
        }
        else
        {
            this.lblMess.Text = "Lỗi trong quá trình cập nhật bản ghi!";
        }
    }
    #endregion

    #region Event Handle
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objChucNang = new DAChucNang(sqlConnec);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            btnUpdate.Visible = false;            
            loadChucNangCha();
            loadViTri();
            loadChucNangContheoChucNangCha();
        }
    }
    protected void ddlChucNangCha_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            loadChucNangContheoChucNangCha();
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
    }
    //Su kien bind du lieu form Chuc Nang
    protected void grvChucNang_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.DataItem != null)
                {
                    e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
                    e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";// Modify
                    // Modify
                    ImageButton lbtDeleteTemp = ((ImageButton)e.Row.FindControl("lbtDelete"));
                    if (lbtDeleteTemp != null)
                    {
                        string strDelete = "Bạn chắc chắn muốn xóa? Chọn OK , Bạn hủy lệnh? Chọn CANCEL ";
                        lbtDeleteTemp.Attributes.Add("Onclick", "javascript:if(confirm('" + strDelete + "')) {return true} else {return false};");
                    }
                }
            }
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
       
    }
    //Su kien xoa 
    protected void grvChucNang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            grvChucNang.EditIndex = -1;
            this.lblMess.Text = "";
            if (Convert.ToInt32(grvChucNang.DataKeys[e.RowIndex].Value) != 48)
            {
                DeleteDanhSachChucNang(e.RowIndex);
                loadChucNangContheoChucNangCha();
            }
            else
            {
                this.lblMess.Text = "Không được xóa chức năng này!";
                return;
            }
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
        
    }
    // Su kien su ly nut Edit trong DataGrid
    protected void grvChucNang_RowEditing(object sender, GridViewEditEventArgs e)
    {
        try
        {
            this.btnInsert.Visible = false;
            this.btnUpdate.Visible = true;
            loadChucNangUpdate(e.NewEditIndex);
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
    }
    protected void grvChucNang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvChucNang.EditIndex = -1;
        this.grvChucNang.PageIndex = e.NewPageIndex;
        loadChucNangContheoChucNangCha();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
           AddDanhSachChucNang();
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        try
        {
            txtTenChucNang.Text = "";
            txtTenForm.Text = "";
            ddlChucNangCha.SelectedValue = "1";
            lblMess.Text = "";
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            UpdateDanhSachChucNang();
        }
        catch (Exception v_e)
        {
            
            throw v_e;
        }
    }
    #endregion  
}